package klk.shop.z.kev.tools;

import java.awt.Color;
import java.io.Serializable;

public class KDate implements Serializable {
	private static final long serialVersionUID = 1L;
	
	
	// Farbdefinition -------------------------------------------------------------------	
	public static final KDate   schwarz     = new KDate(   0,   0,   0 ),
								weiss       = new KDate( 255, 255, 255 ),
	                            rot         = new KDate( 255,   0,   0 ),
	                            tuerkis     = new KDate(   0, 255, 255 ),    
	                            lila     	= new KDate( 255,   0, 255 ),
	                            gruen       = new KDate(   0, 255,   0 ),  
	                            blau        = new KDate(   0,   0, 255 ),                                
	                            gelb        = new KDate( 255, 255,   0 ),
	                            orange     	= new KDate( 255,  63,   0 ),
	                            braun     	= new KDate( 127,  63,   0 ),
	                            hellrot     = new KDate( 255, 127, 127 ),
	                            dunkelgrau	= new KDate(  63,  63,  63 ),
	                            grau        = new KDate( 127, 127, 127 ),
	                            hellgruen	= new KDate( 127, 255, 127 ),  
	                            hellblau 	= new KDate( 127, 127, 255 ),                                
	                            hellgrau 	= new KDate( 191, 191, 191 ),

                                bleu = new KDate(  92, 198, 253 );

	public static final KDate[] c64Farben = new KDate[] {
		KDate.schwarz,					//		Farbe.schwarz,	
		KDate.weiss,					//		Farbe.weiss,	
		new KDate( 104,  55,  43 ),		//		Farbe.rot,		
		new KDate( 122, 191, 199 ),		//		Farbe.tuerkis,		
		new KDate( 111,  61, 134 ),		//		Farbe.lila,		
		new KDate(  88, 141,  67 ),		//		Farbe.gruen,	
		new KDate(  53,  40, 121 ),		//		Farbe.blau,		
		new KDate( 184, 199, 111 ),		//		Farbe.gelb,			
		new KDate( 111,  79,  37 ),		//		Farbe.orange,	
		new KDate(  67,  57,   0 ),		//		Farbe.braun,	
		new KDate( 154, 103,  89 ),		//		Farbe.hellrot,	
		new KDate(  68,  68,  68 ),		//		Farbe.dunkelgrau,	
		new KDate( 108, 108, 108 ),		//		Farbe.grau,		
		new KDate( 154, 210, 132 ),		//		Farbe.hellgruen,
		new KDate( 108,  94, 181 ),		//		Farbe.hellblau,	
		new KDate( 149, 149, 149 )		//		Farbe.hellgrau,		
	};
	
    // Felder --------------------------------------------------------------------------------------
    protected  int rgb = 0;
    // getter fuer eigene Felder -------------------------------------------------------------------
    public     int getRgb() 			{ return rgb; 					}
    // setter fuer eigene Felder -------------------------------------------------------------------
    public    void setRgb(int col)				{ this.rgb = col; 				}
    public    void setRgb(String hexcode) 		{
        if (hexcode.substring(0, 1).equals("#"))
            hexcode = hexcode.substring(1);

        try { setRgb(Integer.parseInt(hexcode, 16)); }
        catch (Exception e) { ; }
    }
    public    void setRgb(int r, int g, int b) 	{ setR(r); setG(g); setB(b); 	}
    // spezielle getter ---------------------------------------------------------------------------
    public     int getR() 				{ return (rgb >> 16) & 255; 	}
    public     int getG() 				{ return (rgb >> 8) & 255;  	}
    public     int getB() 				{ return (rgb) & 255;		  	}
    // spezielle setter ---------------------------------------------------------------------------
    public    void setR(int r) 			{
        if (r < 0) {
            r = 0;
        } else if (r > 255) {
            r = 255;
        }
        rgb = (r << 16) + (getG() << 8) + (getB());
    }
    public    void setG(int g) 			{
        if (g < 0) {
            g = 0;
        } else if (g > 255) {
            g = 255;
        }
        rgb = (getR() << 16) + (g << 8) + (getB());
    }
    public    void setB(int b) 			{
        if (b < 0) {
            b = 0;
        } else if (b > 255) {
            b = 255;
        }
        rgb = (getR() << 16) + (getG() << 8) + (b);
    }
    // --------------------------------------------------------------------------------------------

    // Konvertierungsmethoden ---------------------------------------------------------------------
    public     int toInt() 				{ return rgb; 					}
    public   Color toColor() 			{ return new Color(rgb); 		}
    @Override
    public  String toString() 			{ return toHexString(); 		}
    @Deprecated
    public  String toStringOld() 		{
        return String.format("%d <- %d, %d, %d <- %d, %d, %d", toInt(), getR() << 16, getG() << 8, getB(), getR(), getG(), getB());
    }
    public  String toBinaryString() 	{
        StringBuilder sb = new StringBuilder(Integer.toBinaryString(rgb));
        while ( sb.length() < 25 ) {
            sb.insert( 0, "0" );    //         _???????????????????1001 -> 000000000000000000001001
        }                           // ????????000000000000000000001001 -> 000000000000000000001001
        sb.insert( 9, " " );
        sb.insert( 18, " " );
        return sb.substring( sb.length() - 26, sb.length() );
    }
    public  String toHexString() 		{ return String.format("#%02X%02X%02X", getR(), getG(), getB()); }
    // ---
    public   KDate mischen(KDate f2) 	{ return mischen(this, f2); 								}
    public Boolean isHell() 			{ return ( (getR()+getG()+getB()) > (3*127) ); 				}
    public   KDate aufhellen() 			{ return new KDate(getR() + 16, getG() + 16, getB() + 16); 	}
    public   KDate abdunkeln() 			{ return new KDate(getR() - 16, getG() - 16, getB() - 16); 	}
    public   KDate negativ() 			{ return new KDate(255-getR(), 255-getG() , 255-getB() ); 	}
    public   KDate transform(String s) 	{
    	KDate farbe;
    	if ( s.equals("RBG") ) {
			farbe = new KDate( getR(), getB(), getG() );
		} else if ( s.equals("GBR") ) {
			farbe = new KDate( getG(), getB(), getR() );
		} else if ( s.equals("BRG") ) {
			farbe = new KDate( getB(), getR(), getG() );
		} else if ( s.equals("GRB") ) {
			farbe = new KDate( getG(), getR(), getB() );
		} else {
			farbe = new KDate( getB(), getG(), getR() );
		} 
    	return farbe;
    	
    }
    // -------------------------------------------------------------------------------------------

    // statische Methoden -------------------------------------------------------------------------
    public  static KDate mischen(KDate f1, KDate f2) {
        return new KDate(
                (f1.getR() + f2.getR()) / 2,
                (f1.getG() + f2.getG()) / 2,
                (f1.getB() + f2.getB()) / 2);
    }
    // --------------------------------------------------------------------------------------------
    // override von Standardmethoden --------------------------------------------------------------

    @Override
    public boolean equals(Object o) 	{
        if (o instanceof KDate) {
            return (rgb == ((KDate) o).getRgb());
        }
        return false;
    }
    @Override
    public     int hashCode() 			{
        int hash = 3;
        hash = 79 * hash + this.rgb;
        return hash;
    }
    // --------------------------------------------------------------------------------------------

    // Konstruktoren ------------------------------------------------------------------------------
    public KDate() 						{   this(0, 0, 0); 	}
    public KDate(int r, int g, int b) 	{ 
    	setRgb(r, g, b); 	}
    public KDate(int col) 				{ setRgb(col); 		}
    public KDate(String hexcode) 		{ setRgb(hexcode); 	}
    // --------------------------------------------------------------------------------------------
}
